package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private  RoomLabelService roomLabelService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    /**
     * 分页查询房间信息，注意：这里需要注意到的是，房间图片有多张，如果按照常规去写，那么一个房间会对应一张图片，会出现多条同一间房间的数据，不符合逻辑
     * 因此，我们要使用嵌套查询，即再ResultMap中使用select嵌套查询。
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public BigDecimal getMinRentByApartmentId(Long apartmentId) {
        return roomInfoMapper.getMinRentByApartmentId(apartmentId);
    }

    /**
     * 根据房间id查询房间详情，做回显
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        //实例化返回对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //获取房间基本信息
        RoomInfo roomInfo = this.getById(id);
        if (roomInfo == null) {
            return null;
        }
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //获取公寓的信息
        ApartmentItemVo apartmentItemVo =  apartmentInfoService.getApartmentDetailById(roomInfo.getApartmentId());
        //获取房间图片信息
        List<GraphVo> graphVoList =  graphInfoMapper.getRoomGraphInfoById(id);
        // todo 获取房间属性信息
        List<AttrValueVo> attrValueVoList =  attrValueService.getRoomAttrInfoById(id);
        //获取房间配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityService.getRoomFacilityInfoById(id);
        //获取房间标签信息
        List<LabelInfo> labelInfoList = roomLabelService.getRoomLabelInfoById(id);
        //获取支付方式信息
        List<PaymentType> paymentTypeByRoomId = paymentTypeService.getPaymentTypeByRoomId(id);
        //获取杂费信息  注意：只有公寓有杂费
        List<FeeValueVo> feeValueVoList = feeValueService.getRoomFeeValueInfoByApartmentId(roomInfo.getApartmentId());
        //获取租期列表
        List<LeaseTerm> leaseTermList = leaseTermService.getLeaseTermByRoomid(id);

        //房间是否被删除
        boolean b = (roomInfo.getIsDeleted()) == 1;
        //房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId,roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(queryWrapper);
        Boolean b1 = (count> 0) ? true : false;

        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeByRoomId);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setIsDelete(b);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsCheckIn(b1);


        //这里新增一个保存浏览信息的功能，因为不能影响前端接口的调用，因此做成异步操作
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(),id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> apRoomPage(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.apRoomPage(page,id);
    }


}




